package com.huarun.module.lift.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.github.pagehelper.PageInfo;
import com.huarun.common.BaseController;
import com.huarun.common.ResponseMessage;
import com.huarun.common.util.HttpRequestUtils;
import com.huarun.common.util.ResultCodeEnum;
import com.huarun.common.util.StringUtils;
import com.huarun.module.applyrecord.bean.ApplyRecord;
import com.huarun.module.applyrecord.service.IApplyRecordService;
import com.huarun.module.lift.bean.LiftApply;
import com.huarun.module.lift.service.LiftApplyService;
import com.huarun.module.user.bean.User;
import com.huarun.module.user.service.IUserService;
import io.swagger.annotations.ApiOperation;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({"/wx/liftApply"})
public class LiftApplyController extends BaseController
{

  @Resource
  LiftApplyService applyService;

  @Resource
  private IUserService userService;

  @Resource
  private IApplyRecordService applyRecordService;

  @ApiOperation(value="约梯申请", notes="约梯申请")
  @RequestMapping(value={"/saveLiftApply"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public ResponseMessage<Object> saveLiftApply(HttpServletRequest request, @RequestBody JSONObject param)
  {
    ResponseMessage rm = new ResponseMessage();

    LiftApply lift = (LiftApply)JSON.parseObject(param.toJSONString(), new TypeReference()
    {
    }
    , new Feature[0]);

    System.out.println("+++++++++1");
    if (StringUtils.isBlank(lift.getOnfloor())) {
      rm.setCodeAndMsg(ResultCodeEnum.ERROR.code, "当前的楼层不能为空");
      return rm;
    }
    if (StringUtils.isBlank(lift.getTofloor())) {
      rm.setCodeAndMsg(ResultCodeEnum.ERROR.code, "要去的楼层不能为空");
      return rm;
    }

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    System.out.println("+++++++++2");
    try
    {
      Map userParam = new HashMap();
      userParam.put("openId", request.getHeader("openId"));
      User user = this.userService.getUserByParams(userParam);

      JSONObject code = new JSONObject();
      code.put("EmpNum", user.getWxPhone());
      code.put("onFloor", lift.getOnfloor());
      code.put("toFloor", lift.getTofloor());
      code.put("buildingName", lift.getBuildingName());

      JSONObject qrcodeNoobj = HttpRequestUtils.httpPost("http://132.232.41.235:9998/api/GetliftNo", code);
      String elevatorNum = qrcodeNoobj.getJSONObject("Retdata").getString("elevatorNum");
      lift.setCreateDate(new Date());
      lift.setElevatorPort(elevatorNum);
      lift.setPhone(user.getWxPhone());

      this.applyService.save(lift);
      ApplyRecord entity = new ApplyRecord();
      entity.setApplyId(lift.getId());
      entity.setUserId(user.getId());
      entity.setTitle("即时约梯申请");

      entity.setApplyType("即时约梯申请");
      entity.setTypeCode(Integer.valueOf(6));
      entity.setStatus("已提交");
      entity.setCreateDate(new Date());

      this.applyRecordService.save(entity);
      rm.setCodeAndMsg(ResultCodeEnum.SUCCESS.code, ResultCodeEnum.SUCCESS.msg);
      Map state = new HashMap();
      state.put("id", lift.getId());
      if (!elevatorNum.equals(""))
        state.put("state", Integer.valueOf(0));
      else {
        state.put("state", Integer.valueOf(1));
      }
      rm.setData(state);
      rm.setCodeAndMsg(ResultCodeEnum.SUCCESS.code, ResultCodeEnum.SUCCESS.msg);
    } catch (Exception e) {
      rm.setCodeAndMsg(ResultCodeEnum.ERROR.code, ResultCodeEnum.ERROR.msg);
      this.log.error("申请异常！");
      e.printStackTrace();
    }

    return rm;
  }
  @ApiOperation(value="约梯详情", notes="约梯详情")
  @RequestMapping(value={"/getLiftApplyDetail"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public ResponseMessage<Map<String, Object>> getVisitorInviteDetail(HttpServletRequest req, @RequestBody Map<String, Object> map) { ResponseMessage rm = new ResponseMessage();
    try
    {
      Map inviteMap = this.applyService.getLiftApplyId(map);
      rm.setData(inviteMap);
      rm.setCodeAndMsg(ResultCodeEnum.SUCCESS.code, ResultCodeEnum.SUCCESS.msg);
    }
    catch (Exception e) {
      rm.setCodeAndMsg(ResultCodeEnum.ERROR.code, ResultCodeEnum.ERROR.msg);
      this.log.error("约梯申请详情异常！");
      e.printStackTrace();
    }
    return rm; }

  @ApiOperation(value="约梯申请列表", notes="约梯申请列表")
  @RequestMapping(value={"/getLiftApplyList"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public ResponseMessage<PageInfo<Map<String, Object>>> getLiftApplyList(HttpServletRequest req, @RequestBody Map<String, Object> map, @RequestParam(required=false) Integer page, @RequestParam(required=false) Integer rows) {
    ResponseMessage rm = new ResponseMessage();
    try {
      Map userParam = new HashMap();
      userParam.put("openId", req.getHeader("openId"));
      User user = this.userService.getUserByParams(userParam);
      Integer userId = user.getId();
      map.put("userId", userId);
      PageInfo inviteMap = this.applyService.getLiftApplyList(map, page, rows);
      rm.setData(inviteMap);
      rm.setCodeAndMsg(ResultCodeEnum.SUCCESS.code, ResultCodeEnum.SUCCESS.msg);
    }
    catch (Exception e) {
      rm.setCodeAndMsg(ResultCodeEnum.ERROR.code, ResultCodeEnum.ERROR.msg);
      this.log.error("约梯申请详情异常！");
      e.printStackTrace();
    }
    return rm; } 
  @ApiOperation(value="取消约梯", notes="取消约梯")
  @RequestMapping(value={"/cancelLiftApply"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public ResponseMessage<Object> cancelLiftApply(HttpServletRequest req, @RequestBody Map<String, Object> map) { ResponseMessage rm = new ResponseMessage();
    try {
      map.put("updateDate", new Date());
      map.put("status", "已取消");
      int flag = this.applyService.cancelLiftApply(map);
      if (flag > 0);
      rm.setData("取消成功");
      rm.setCodeAndMsg(ResultCodeEnum.SUCCESS.code, ResultCodeEnum.SUCCESS.msg);
    }
    catch (Exception e) {
      rm.setCodeAndMsg(ResultCodeEnum.ERROR.code, ResultCodeEnum.ERROR.msg);
      this.log.error("邀约申请详情异常！");
      e.printStackTrace();
    }
    return rm;
  }
}

/* Location:           D:\msg\反编译\Webuilding-0.0.1-SNAPSHOT(5)\BOOT-INF\classes\
 * Qualified Name:     com.huarun.module.lift.controller.LiftApplyController
 * JD-Core Version:    0.6.2
 */